<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/logo2.png" />
        <span>电商后台管理系统</span>
      </div>
      <el-button @click="logout" type="info">退出</el-button>
    </el-header>
    <el-container>
      <!-- 左侧菜单 -->
      <el-aside :width="iscollapse?'64px':'200px'">
        <div @click="toggle" class="toggle-button">|||</div>
        <el-menu
          router
          :collapse-transition="false"
          :collapse="iscollapse"
          unique-opened
          background-color="#f7f7f7"
          active-text-color="orange"
          :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <el-submenu v-for="(item) in MenuList" :key="item.id" :index="item.id+''">
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              v-for="(subitem) in item.children"
              :key="subitem.id"
              :index="'/'+subitem.path"
              @click="saveNavState('/'+subitem.path)"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{subitem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 内容区 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  data() {
    return {
      MenuList: [],
      iconsObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      iscollapse: false,
      activePath: ''
    }
  },
  created() {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout() {
      window.sessionStorage.clear();
      this.$router.push('/login')
    },
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.mata.mes)
      this.MenuList = res.data
    },
    toggle() {
      this.iscollapse = !this.iscollapse
    },
    saveNavState(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>
<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  font-size: 22px;
  color: white;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  div {
    display: flex;
    height: 60px;
    line-height: 60px;
    span {
      margin-left: 10px;
      a {
        text-decoration: none;
        color: white;
      }
    }
  }
}
.el-aside {
  background-color: #f7f7f7;
  box-shadow: 0 0 5px #cccccc;
  .el-menu {
    border-right: none;
  }
}
.toggle-button {
  background-color: #f7f7f7;
  height: 30px;
  border-bottom: 1px solid #c9c9c9;
  font-size: 12px;
  text-align: center;
  line-height: 30px;
  letter-spacing: 0.2em;
  cursor: pointer;
}
.iconfont {
  margin-right: 10px;
}
</style>
